//
//  STLogView.swift
//  iPhone
//
//  Created by Toj on 8/28/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STLogView: UIView {
    
    private var logs: [String] = []
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    /* <#NOTE#> */
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.st_alpha(0.3)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapClose))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.backgroundColor = .black
        view.rowHeight = 25
        view.dataSource = self
        view.register(cellClass: STLogCell.self)
        
        return view
    }()
    
    /* <#NOTE#> */
    private lazy var clearButton: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = 13.to_systemFont
        view.setTitle("清空", for: .normal)
        view.addTarget(self, action: #selector(didTapClear), for: .touchUpInside)
        return view
    }()
    
    /* <#NOTE#> */
    private lazy var toTopButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.titleLabel?.font = 13.to_systemFont
        view.setTitle("顶部", for: .normal)
        view.addTarget(self, action: #selector(didTapToTop), for: .touchUpInside)
        return view
    }()
    
    /* <#NOTE#> */
    private lazy var toBottomButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.titleLabel?.font = 13.to_systemFont
        view.setTitle("底部", for: .normal)
        view.addTarget(self, action: #selector(didTapToBottom), for: .touchUpInside)
        return view
    }()
    
    deinit {
        print("-->toj: \(cm_className) func: deinit")
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STLogView {
    
    func didTapClose() {
        isHidden = true
    }
    
    func didTapClear() {
        clear()
    }
    
    func didTapToTop() {
        let isAnimated = arc4random_uniform(2) == 1
        tableView.scrollToTopRow(animated: isAnimated)
    }
    
    func didTapToBottom() {
        let isAnimated = arc4random_uniform(2) == 1
        tableView.scrollToBottomRow(animated: isAnimated)
    }
}

// MARK: - Public
extension STLogView {
    
    func log(add data: String) {
        let index = logs.count
        logs.append(data)
        tableView.insertRows(at: [IndexPath(row: index, section: 0)], with: .bottom)
        didTapToBottom()
        // logs.insert(data, at: 0)
        // tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .top)
        // didTapToTop()
    }
    
    func log(add datas: [String]) {
        logs.append(contentsOf: datas)
        tableView.reloadSections(IndexSet(integer: 0), with: .bottom)
        didTapToBottom()
        // logs.insert(contentsOf: datas, at: 0)
        // tableView.reloadSections(IndexSet(integer: 0), with: .top)
        // didTapToTop()
    }
    
    func clear() {
        logs = ["准备就绪"]
        tableView.reloadData()
    }
}

// MARK: - Private
private extension STLogView {
    
}

// MARK: - UITableViewDataSource
extension STLogView: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return logs.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withReuseIdentifierClass: STLogCell.self)
        cell.log = logs[indexPath.row]
        return cell
    }
}

// MARK: - 初始化设置
private extension STLogView {
    
    func setupMakeAddSubviews() {
        addSubview(backgroundView)
        addSubview(tableView)
        addSubview(clearButton)
        addSubview(toTopButton)
        addSubview(toBottomButton)
    }
    
    func setupMakeLayoutSubviews() {
        backgroundView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        tableView.snp.makeConstraints { make in
            make.right.top.bottom.equalTo(self)
            make.width.equalTo(self).multipliedBy(0.8)
        }
        
        clearButton.snp.makeConstraints { make in
            make.right.bottom.equalTo(self).inset(10)
            make.width.equalTo(50)
            make.height.equalTo(30)
        }
        
        var y = 100
        toTopButton.snp.makeConstraints { make in
            make.top.equalTo(y)
            make.left.equalTo(self)
            make.height.equalTo(44)
            make.width.equalTo(self).multipliedBy(0.2)
        }
        y += 100
        toBottomButton.snp.makeConstraints { make in
            make.top.equalTo(y)
            make.left.width.height.equalTo(toTopButton)
        }
    }
    
    func setupMakeInitialize() {
        clear()
    }
}
